#    This file is part of robo-ircbot.
#
#    robo-ircbot is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    robo-ircbot is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with robo-ircbot.  If not, see <http://www.gnu.org/licenses/>.

package App::Modular::Module::Basic;
use strict;
use warnings;
use base qw(App::Modular::Module);

print ":: Basic module loaded.\n";

# Events you would like to receive.
# (all scripts need this method)
sub events { return "PRIVMSG RAW PING" }

sub help {
        return (
                "Basic core functionality."
        );
}

# PRIVMSG events.
sub event_privmsg
{
        shift;
        my $e = shift;

        # If $to is equal to my nick...
        if ($e->to eq $e->cfg->nick) {
                # CTCP version request.
                if ($e->data eq "\1VERSION\1") {
                        $e->from =~ /(.+)!.+/;

                        # Send version reply.
                        $e->robo->log(":: Sending VERSION reply.");
                        $e->s("NOTICE ".$1." :\1VERSION robo v".
                              $e->cfg->get_cfg("version").
                              " - pwnagest b0t in teh w0rld.\1");
                }
        }
}

# RAW events.
sub event_raw
{
        shift;
        my $e = shift;

        # End of /MOTD command, so lets join our channel(s).
        if ($e->raw == 376 || $e->raw == 422) {
                # Multiple channel blocks.
                if (ref($e->cfg->get_cfg("channel")) eq 'ARRAY') {
                        foreach (@{$e->cfg->get_cfg("channel")}) {
                                $e->robo->log(":: Joining ".$_->{chan});
                                $e->s("JOIN ".$_->{chan}." ".$_->{pass});
                        }
                # Single channel block.
                } else {
                        $e->robo->log(":: Joining ".$e->cfg->get_cfg("channel/chan"));
                        $e->s("JOIN ".$e->cfg->get_cfg("channel/chan")." ".
                              $e->cfg->get_cfg("channel/pass"));
                }

        # Nickname is already in use.
        } elsif ($e->raw == 433) {
                $e->robo->log(":: Nick in use, using ".$e->to."_");
                $e->s(        "NICK ".$e->to."_".
                                "\r\nUSER ".$e->cfg->get_cfg("bot/user_name").
                                " 0 0 :".$e->cfg->get_cfg("bot/real_name"));

                # Record what our current nick is.
                $e->cfg->nick($e->to."_");
        }
}

# PING events.
sub event_ping
{
        shift;
        my $e = shift;

        # Send back a PONG.
        $e->robo->log(":: PONG ".$e->data);
        $e->s("PONG ".$e->data);
}

1;
